class otherHome extends eui.Component{
    private energyGroup:eui.Group;
    private addFriend:eui.Image;
    private area:eui.Group;
    private other:eui.Group;
    private goback:eui.Button;
    private toggle:eui.Image;
    private otherTop:eui.Group;
    private topGroup:eui.Group;
    private list:eui.List;
    private _arrayData:eui.ArrayCollection;
    private _listData:Array<any> = [];

    private _isFriend:number = 0;
    private _userId:number = 0;
    //isFriend 0:是朋友，1：不是朋友

    private headMask:eui.Image;
    private head:eui.Image;
    private nickName:eui.Label;
    private title:eui.Label;
    private project:eui.Rect;
    private exp:eui.Label;
    private energy:eui.Label;
    private born:eui.Label;
    private _userInfo:any;
    private _plantUI:plantSub;
    private plantIndex:Array<any> = [{x:333,y:701},{x:236,y:748},{x:431,y:748},{
        x:144,y:798},{x:333,y:798},{x:525,y:798},{x:236,y:848},{x:431,y:848},{x:333,y:897}];
    private plant:Array<plantInfom> = [];//植物种植

    private _hide:boolean = false;

    //pet
    private mcFactory:egret.MovieClipDataFactory;
    private pet:egret.MovieClip;
    public constructor(isFriend:number=0,userId:number){
        super();
       // this.cacheAsBitmap =true;
        this._isFriend = isFriend;
        this._userId = userId;
        this.addEventListener(eui.UIEvent.COMPLETE , this.createCompleteEvent,this);
        this.skinName = "homeSkin"; 
    }

    private createCompleteEvent():void{
        this.removeEventListener(eui.UIEvent.COMPLETE , this.createCompleteEvent, this);

        Dispatcher.ins.addEventListener("userInfo", this.resetInfo, this);
        Dispatcher.ins.addEventListener("plantList", this.draw, this);
        Dispatcher.ins.addEventListener("otherHomeDraw",this.getPlantList,this);
        Dispatcher.ins.addEventListener("friendList", this.setList, this);
        Dispatcher.ins.addEventListener("addFriend", ()=>{
            this.addFriend.visible = false;
            this.getList();
        } , this);

        this.bgActionInit();
        this.Init();
    }
    private async Init(){
        this.area.visible = false;
        this.other.visible = true;
        this.topGroup.visible = false;
        this.energyGroup.visible = false;
        if(this._isFriend == 0){
            this.addFriend.visible = false;
        }else{
            this.addFriend.visible = true;
        }
        this.goback.addEventListener(egret.TouchEvent.TOUCH_TAP,()=>{
            this.parent.removeChild(this);
        },this);
        this.addFriend.addEventListener(egret.TouchEvent.TOUCH_TAP,()=>{
            let dataPost = {"addUserId":this._userId}
            socket.request("addFriend",dataPost);
        },this);
        this.toggle.addEventListener(egret.TouchEvent.TOUCH_TAP,()=>{
            if(this._hide){
                egret.Tween.get(this.otherTop).to({x:0},500).call(()=>{
                    
                });
                egret.Tween.get(this.toggle).to({rotation:180},500);
            }else{
                egret.Tween.get(this.otherTop).to({x:125},500).call(()=>{
                    
                });
                egret.Tween.get(this.toggle).to({rotation:0},500);
            }
            this._hide = !this._hide;
        },this);

        this._arrayData =  new eui.ArrayCollection(this._listData);
        this.list.dataProvider = this._arrayData;
        this.list.itemRenderer = homeOtherIR;

        this.getInfo(this._isFriend,this._userId);

        this.getList();
    }
    public getInfo(isFriend:number=0,userId:number):void{
        this._isFriend = isFriend;
        this._userId = userId;
        if(this._isFriend == 0){
            this.addFriend.visible = false;
        }else{
            this.addFriend.visible = true;
        }
        let dataPost = {"userId":this._userId};
        socket.request("userInfo",dataPost);
        this.getList();
    }
    private resetInfo(evt:GameEvent):void{
        let obj = evt.data;
        this.head.mask = this.headMask;
        this.head.source = obj.Head;
        this.nickName.text = obj.Nickname.length > 5 ? obj.Nickname.substr(0,4)+"..." : obj.Nickname;
        this.title.text = obj.Name + "lv" + obj.Lv;
        this.exp.text = obj.Exp + "/" + obj.MaxExp;
        this.project.width = (obj.Exp / obj.MaxExp) * 145;
        this.energy.text = common.userInfo.energy + "";
        this._userId = obj.UserId;
        this.born.text = "";
        if(obj.isGm){
            this._isFriend = 0;
            this.addFriend.visible = false;
        }else{
            this._isFriend = 1;
            this.addFriend.visible = true;
        }
        if(obj.petId !== 0){
            this.createPet(obj.petId);
        }else{
            if(this.pet && this.pet.parent){
                this.removeChild(this.pet);
            }
        }
        this.getPlantList();
    }
    private getPlantList():void{
        let dataPost = {"userId":this._userId};
        socket.request("plantList",dataPost,"otherHome");
    }
    public getList():void{
        let dataPost = {}
        let note = "otherHome";
        socket.request("friendList",dataPost,note);
    }
    public setList(evt:GameEvent):void{
        let obj = evt.data;
        if(obj.note !== "otherHome") return;
        let list = obj.list;
        this._listData = list;
        this._arrayData =  new eui.ArrayCollection(this._listData);
        this.list.dataProvider = this._arrayData;
        this._arrayData.refresh();
    }
    public draw(evt:GameEvent){
        let obj = evt.data;
        let flower = obj.list;
        common.otherPlant = [];
        //土地信息初始化
        if(this.plant.length > 0){
            for(let i = 0;i < 9;i ++){
                this.plant[i].url = "";
            }
        }else{
            for(let i = 0;i < 9;i++){
                let fl:plantInfom = new plantInfom();
                fl.Id = i;
                fl.x = this.plantIndex[i].x;
                fl.y = this.plantIndex[i].y;
                this.plant.push(fl);
                this.plant[i].addEventListener(egret.TouchEvent.TOUCH_TAP,this.plantAnaly,this);
                this.addChild(this.plant[i]);
            }
        }
        
        for(let i = 0,lg = flower.length;i < lg;i++){
            let obj = flower[i];
            this.plant[obj.IndexId].Id = obj.IndexId;
            this.plant[obj.IndexId].name = obj.PName;
            this.plant[obj.IndexId].url = "fl_"+obj.PId+"_"+obj.GrowStage+"_png";
            this.plant[obj.IndexId].growStage = obj.GrowStage;
            this.plant[obj.IndexId].nEx = obj.CurGrow;
            this.plant[obj.IndexId].mEx = obj.Grow3 + 1;
            this.plant[obj.IndexId].plantId = obj.Id;
            this.plant[obj.IndexId].index = i;
            common.otherPlant.push(obj);
        }
    }
    private plantAnaly(evt:egret.TouchEvent){
        let target = evt.currentTarget;
         if(target.url.length == 0){
            
        }else{
            let _name = target.name;//没有name
            let _show = target.url;
            let _plantId = target.plantId;
            let _id = target.Id;
            let _curGrow = target.nEx;
            let _maxGrow = target.mEx;
            let _growStage = target.growStage;//当前阶段
            let _index = target.index;
            if(!this._plantUI){
                this._plantUI = new plantSub(_index,_name,_show,_id,_plantId,_curGrow,_maxGrow,_growStage,1,this._userId);
                this._plantUI.addEventListener("return",()=>{
                    this.removeChild(this._plantUI);
                },this);
            }else{
                this._plantUI.anayData(_index,_name,_show,_id,_plantId,_curGrow,_maxGrow,_growStage,1,this._userId);
            }
            this.addChild(this._plantUI);
        }
    }
    private bgActionInit():void{
        var data = RES.getRes("action_json");
        var txtr = RES.getRes("action_png");
        var action = new egret.MovieClipDataFactory( data, txtr );
        var windmill = new egret.MovieClip(  action.generateMovieClipData( "windmill" ) );
        windmill.x = 65;
        windmill.y = 160;
        windmill.scaleX = 0.75;
        windmill.scaleY = 0.75;
        this.addChild(windmill);
        windmill.gotoAndPlay(0,-1);
    }
    private createPet(id):void{
        //宠物
        if(!this.mcFactory){
            var data = RES.getRes("my_pet_json");
            var txtr = RES.getRes("my_pet_png");
            this.mcFactory = new egret.MovieClipDataFactory( data, txtr );
        }
        if(this.pet && this.pet.parent){
            this.removeChild(this.pet);
            this.pet = null;
        }
        this.pet = new egret.MovieClip( this.mcFactory.generateMovieClipData( "pet_"+id ) );
        this.addChild(this.pet);
        this.pet.x = 280;
        this.pet.y = 420;
        this.pet.scaleX = 0.6;
        this.pet.scaleY = 0.6;
        this.pet.gotoAndPlay( 1 ,3);
        this.pet.touchEnabled = true;
        this.pet.addEventListener(egret.TouchEvent.TOUCH_TAP,()=>{
            let num = Math.ceil(Math.random() * 5);
            this.pet.gotoAndPlay( 1 ,num);
        },this);
    }
}
class homeOtherIR extends eui.ItemRenderer{
    public constructor(){
        super();
        this.addEventListener(eui.UIEvent.COMPLETE , this.createCompleteEvent,this);
        this.skinName = "homeOtherIRSkin";
    }
    private createCompleteEvent():void{
        super.createChildren();
        this.removeEventListener(eui.UIEvent.COMPLETE , this.createCompleteEvent, this);
        this.addEventListener(egret.TouchEvent.TOUCH_TAP,()=>{
            let dataPost = {"userId":this.data.userId}
            socket.request("userInfo",dataPost);
        },this);
    }
    protected dataChanged():void{

    }
}